package com.test.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPEchoServerPool {

	public static void main(String[] args)throws IOException{
		int echoServPort = Integer.parseInt("8086");
		int threadPoolSize = Integer.parseInt("10");
		
		//create a server socket to accept client connection requests
		final ServerSocket serSock = new ServerSocket(echoServPort);
		
		final Logger logger = Logger.getLogger("pratical");
		//spawn a fixed number of threads to service clients
		for (int i = 0; i < threadPoolSize; i++) {
			Thread thread = new Thread(){

				@Override
				public void run() {
					while(true){
						try {
							Socket clntSock = serSock.accept(); //wait for a connection
							EchoProtocol.handleEchoClient(clntSock, logger); //handle it
						} catch (IOException e) {
						logger.log(Level.WARNING, "Client accept failed",e);
						}
					}
				}
				
			};
			thread.start();
			logger.info("Create and started Thread = "+thread.getName());
		}
	}
}
